// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: B08 Eggman Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// Aliases
private alias 70 : TYPE_B08EGGMAN

private alias HPZSetup_value34 : HPZSetup_bossEncounterFlag

// Function declarations
reserve function B08Eggman_Function146
reserve function B08Eggman_Function147
reserve function B08Eggman_Function148

// Tables
public table B08Eggman_table50
	7, 7, 8, 8
end table

public table B08Eggman_table51
	9, 9, 10, 10
end table

public table B08Eggman_table52
	11, 11, 12, 12
end table


function B08Eggman_Function146
	Sin256(object.ypos, object.value2)
	object.ypos <<= 10
	object.ypos += object.value1
	object.value2 += 2
	object.value2 &= 255
end function


function B08Eggman_Function147
	object[+1].xpos = object.xpos
	object[+1].ypos = object.ypos
	object[+1].direction = object.direction
	object[+2].xpos = object.xpos
	object[+2].ypos = object.ypos
	object[+2].direction = object.direction
end function


function B08Eggman_Function148
	checkResult = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object[currentPlayer].yvel < 0
			checkResult |= 1
		end if
	next
	if checkResult == 1
		object.value1 -= 0xC000
		if object.value1 < object.value10
			object.value1 = object.value10
		end if
		object.value13 = 3
	else
		object.value1 += 0x8000
		if object.value1 > object.value9
			object.value1 = object.value9
		end if
		object.value13 = 1
	end if
	temp0 = object[0].xpos
	temp0 -= 0x80000
	temp1 = object[0].xpos
	temp1 += 0x80000
	if object.xpos < temp0
		object.xpos += 0x10000
		object.direction = FLIP_X
	end if
	if object.xpos > temp1
		object.xpos -= 0x10000
		object.direction = FLIP_NONE
	end if
end function


event ObjectMain
	switch object.state
	case 0
		if object[0].xpos > object[+1].xpos
			temp0 = object[+1].ixpos
			temp0 -= screen.xcenter
			stage.newXBoundary1 = temp0
			temp0 = object[+1].ixpos
			temp0 += screen.xcenter
			stage.newXBoundary2 = temp0
			temp0 = stage.newYBoundary2
			temp0 -= screen.ysize
			stage.newYBoundary1 = temp0
			object.value6 = object[+1].xpos
			object.value7 = -112
			object.value7 *= screen.xsize
			object.value7 /= 320
			object.value7 <<= 16
			object.value8 = object.value7
			FlipSign(object.value8)
			object.value7 += object[-1].xpos
			object.value8 += object[-1].xpos
			object.value9 = object[+1].ypos
			object.value10 = object.value9
			object.value10 -= 0x200000
			object.value11 = object[+2].ypos
			object.xpos = 72
			object.xpos *= screen.xsize
			object.xpos /= 320
			object.xpos <<= 16
			object.xpos += object[+1].xpos
			object.ypos = object.value9
			object.ypos += 0xF00000
			object.animation = 1
			ResetObjectEntity(26, TypeName[Music Event], 0, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object[+1].type = TypeName[Eggman Horn]
			object[+2].type = TypeName[Eggman Valve]
			CallFunction(B08Eggman_Function147)
			object.value17 = -0x280000
			object.value20 = 1
			object.value13 = 2
			HPZSetup_bossEncounterFlag = true
			object.value24 = object[-1].iypos
			stage.waterLevel = object.value24
			stage.waterLevel += 192
			object.priority = PRIORITY_ACTIVE
			object.state = 1
		end if
		break
	case 1
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object.yvel = 0
			object.state = 2
		end if
		break
	case 2
		object.yvel -= 0x1800
		if object.yvel < -0x20000
			object.yvel = -0x20000
		end if
		object.ypos += object.yvel
		if stage.waterLevel > object.value24
			stage.waterLevel -= 2
		end if
		if object.ypos <= object.value9
			object.ypos = object.value9
			object.value1 = object.ypos
			object.value3 = 8
			object.state = 3
		end if
		CallFunction(B08Eggman_Function147)
		break
	case 3
		object.value0++
		if object.value17 < -0x280000
			object.value17 += 0x20000
		end if
		if object.value0 == 240
			object.value0 = 0
			object.value23 = object.entityPos
			object.value23 -= 4
			object.value18 = -0x280000
			object.state = 4
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 4
		temp0 = object.value0
		temp0 %= 60
		if temp0 == 0
			object[+1].state = 1
			object[+1].value0 = 30
			object[+2].state++
			if object[+2].state == 3
				object[+2].state = 1
			end if
			temp1 = object.value0
			temp1 /= 60
			switch temp1
			case 0
				PlaySfx(SfxName[Horn 1], 0)
				break
			case 1
				PlaySfx(SfxName[Horn 2], 0)
				break
			case 2
				PlaySfx(SfxName[Horn 3], 0)
				break
			case 3
				PlaySfx(SfxName[Horn 4], 0)
				break
			end switch
		end if
		if object[+1].value0 == 1
			temp1 = screen.xsize
			temp1 *= 224
			temp1 >>= 8
			Rand(temp0, temp1)
			temp0 >>= 6
			temp0 <<= 6
			temp1 >>= 1
			temp0 -= temp1
			temp0 <<= 16
			temp0 += object.value6
			foreach (TypeName[Eggman Mine], arrayPos0, ACTIVE_ENTITIES)
				temp1 = object[arrayPos0].xpos
				temp1 -= temp0
				Abs(temp1)
				if temp1 <= 0x180000
					if temp0 < object.value6
						temp0 += 0x200000
					else
						temp0 -= 0x200000
					end if
				end if
			next
			arrayPos0 = object.value23
			ResetObjectEntity(object.value23, TypeName[Eggman Mine], 0, temp0, object.value10)
			object[arrayPos0].ypos -= 0x400000
			object.value23++
			object.value18 -= 0xA0000
		end if
		if object[+1].value0 == 0
			if object.value17 > object.value18
				object.value17 -= 0x10000
			end if
		end if
		object.value0++
		if object.value0 == 240
			object.value0 = 0
			object.state = 5
			object[+2].state = 0
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 5
		object.value0++
		if object.value0 == 160
			object.value0 = 0
			object.angle = 256
			object[+1].state = 2
			object[+1].value0 = 128
			object.state = 6
			PlaySfx(SfxName[Horn Sweep], 0)
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 6
		object.angle += 4
		Cos(stage.waterLevel, object.angle)
		stage.waterLevel >>= 4
		FlipSign(stage.waterLevel)
		stage.waterLevel -= 32
		temp0 = stage.waterLevel
		temp0 *= 3
		temp0 /= 6
		object.value17 = -80
		object.value17 -= temp0
		object.value17 <<= 16
		stage.waterLevel += object.value24
		if object.angle == 768
			object[-1].state = 4
			object[-2].state = 4
			object[-3].state = 4
			object[-4].state = 4
			object[+2].state = 0
			object.state = 7
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 7
		object.value0++
		if object.value17 < -0x280000
			object.value17 += 0x20000
		end if
		if object.value0 == 240
			object.value0 = 0
			object[+1].state = 1
			object[+1].value0 = 240
			object[+2].state = 3
			object.state = 8
			PlaySfx(SfxName[Loud Horn], 0)
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 8
		if object.value17 > -0x500000
			object.value17 -= 0x40000
		end if
		if object[+1].value0 == 0
			object.state = 9
			object[+2].state = 0
			arrayPos0 = object.entityPos
			arrayPos0--
			ResetObjectEntity(arrayPos0, TypeName[Eggman Mine], 2, object[0].xpos, object.value10)
			object[arrayPos0].ypos -= 0x400000
		end if
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			temp0 = oscillation
			temp0 &= 15
			if temp0 == 0
				temp0 = 10
			else
				temp0 = 11
			end if
			Rand(temp1, screen.xsize)
			temp1 -= screen.xcenter
			temp1 <<= 16
			temp1 += object.value6
			temp2 = object.value9
			temp2 -= 0x500000
			CreateTempObject(TypeName[Break Gem], temp0, temp1, temp2)
			object[tempObjectPos].value1 = 0x2800
			object[tempObjectPos].state = 1
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 9
		if object[-1].type == TypeName[Blank Object]
			if object[-2].type == TypeName[Blank Object]
				object.state = 3
			end if
		end if
		CallFunction(B08Eggman_Function148)
		CallFunction(B08Eggman_Function146)
		CallFunction(B08Eggman_Function147)
		break
	case 10
		object[arrayPos0].value13 = 0
		switch object.value16
		case 0
			object.xpos += 0x20000
			break
		case 2
			object.xpos -= 0x20000
			break
		end switch
		object.animation = 3
		object.value16++
		object.value16 &= 3
		CallFunction(B08Eggman_Function147)
		break
	case 11
		object.ypos += object.yvel
		object.yvel += 0x1800
		if object.value25 < 3
			if object.ypos > object.value11
				object.ypos = object.value11
				object.yvel >>= 1
				FlipSign(object.yvel)
				object.value25++
				PlaySfx(SfxName[Ground Impact], 0)
			end if
		else
			if object.yvel > 0
				object.value25 = 0
				object.value13 = 1
				object.state = 12
			end if
		end if
		CallFunction(B08Eggman_Function147)
		break
	case 12
		object.yvel -= 0x1800
		if object.yvel < -0x20000
			object.yvel = -0x20000
			object.value13 = 2
		end if
		object.ypos += object.yvel
		if object.ypos <= object.value9
			object.ypos = object.value9
			object.value1 = object.ypos
			object.value2 = 0
			object.state = 3
		end if
		CallFunction(B08Eggman_Function147)
		break
	case 13
		object.value0++
		if object.value0 == 30
			object.value21 = -0x20000
			object.value22 = 0
		end if
		if object.value20 == 1
			object.value17 += object.value21
			if object.value17 < -0x500000
				object.value22 += 0x3800
				object.value19 += object.value22
			end if
		end if
		if object.value0 == 60
			if object.direction == FLIP_NONE
				object[+1].xvel = -0x10000
			else
				object[+1].xvel = 0x10000
			end if
			object[+1].yvel = -0x20000
			object[+1].state = 3
		end if
		if object.value0 == 120
			object.value20 = 0
			if object.direction == FLIP_NONE
				object[+2].xvel = 0x10000
			else
				object[+2].xvel = -0x10000
			end if
			object[+2].yvel = -0x20000
			object[+2].state = 4
		end if
		if object.value0 == 180
			object.value0 = 0
			object.animation = 5
			object.value5 = 0
			object.yvel = 0
			object.value13 = 0
			HPZSetup_bossEncounterFlag = false
			object.state = 14
			ResetObjectEntity(26, TypeName[Music Event], 1, 0, 0)
			object[26].priority = PRIORITY_ACTIVE
			object[+3].priority = PRIORITY_ACTIVE
			object[+3].state = 3
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break
	case 14
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.value0++
		if object.value0 == 16
			object.yvel = 0
			object.value0 = 0
			object.animationTimer = 0
			object.value13 = 1
			object.state++
		end if
		break
	case 15
		if object.value0 < 24
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.value0++
		else
			object.value0 = 0
			object.yvel = 0
			object.value1 = object.ypos
			object.state++
		end if
		break
	case 16
		CallFunction(B08Eggman_Function146)
		if object.value0 < 8
			object.value0++
		else
			object.value0 = 0
			object.animation = 6
			object.value13 = 2
			object.animationTimer = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break
	case 17
		object.xpos += 0x40000
		object.value1 -= 0x4000
		CallFunction(B08Eggman_Function146)
		if object.outOfBounds == 1
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		end if
		break
	end switch
	if object.value3 != 0
		if object.value4 > 0
			object.value4--
			GetBit(temp0, object.value4, 0)
			if temp0 == 1
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0)
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.value4 < 32
				CheckEqual(object[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(object[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != 2
						object.animation = 2
						object.animationTimer = 0
					end if
				end if
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 20, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
				if checkResult == 1
					CallFunction(PlayerObject_CheckHit)
					if checkResult == 1
						object.value3--
						if object.value3 == 0
							player.score += 1000
							object.value0 = 0
							object.animation = 4
							object.value13 = 1
							object.animationTimer = 0
							object.value5 = 1
							object.value1 = object.ypos
							object.state = 13
							SetPaletteEntry(0, 192, 0)
						else
							object.animation = 3
							object.animationTimer = 0
							object.value4 = 64
							PlaySfx(SfxName[Boss Hit], 0)
						end if
					end if
				end if
				if object.value13 == 3
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, 0, 8, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						CallFunction(PlayerObject_Hit)
					end if
				end if
			end if
		next
	end if
	if object.state != 17
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += object[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				object[currentPlayer].xvel = 0
				object[currentPlayer].speed = 0
				object[currentPlayer].xpos = temp1
				temp0 = object[currentPlayer].collisionRight
				temp0 <<= 16
				object[currentPlayer].xpos -= temp0
			end if
		next
	end if
	if object.value5 == 1
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], 0)
		end if
	end if
end event


event ObjectDraw
	temp2 = object.value17
	FlipSign(temp2)
	temp2 >>= 16
	if temp2 < 0
		temp2 = 0
	end if
	if temp2 > 80
		temp2 = 80
	end if
	temp3 = object.value17
	temp3 >>= 16
	temp3 -= 16
	temp4 = object.value19
	temp4 += object.ypos
	if object.value20 == 1
		EditFrame(13, temp3, 0, temp2, 6, 265, 235)
		DrawSpriteFX(13, FX_FLIP, object.xpos, temp4)
	end if
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
	switch object.animation
	case 1
		temp0 = object.animationTimer
		temp0 >>= 3
		if saveRAM[495] == 0
			object.animationTimer++
			object.animationTimer %= 15
		endif
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if object[0].animation != ANI_DYING
				object.animation = 1
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = 1
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break
	case 4
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 5
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break
	case 6
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(15, FX_FLIP, object.xpos, object.ypos)
		end if
		break
	end switch
	if object.value20 == 1
		EditFrame(14, temp3, 6, temp2, 6, 265, 241)
		DrawSpriteFX(14, FX_FLIP, object.xpos, temp4)
	end if
	switch object.value13
	case 0
		break
	case 1
		GetTableValue(temp0, object.value14, B08Eggman_table50)
		object.value14++
		object.value14 &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		GetTableValue(temp0, object.value14, B08Eggman_table51)
		object.value14++
		object.value14 &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		GetTableValue(temp0, object.value14, B08Eggman_table52)
		object.value14++
		object.value14 &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
	end switch
end event


event ObjectStartup
			if saveRAM[495] == 0
				LoadSpriteSheet("HPZ/Objects.gif")
			else
				LoadSpriteSheet("HPZ/ObjectsE.gif")
			endif
	SpriteFrame(-28, -28, 60, 20, 265, 139)
	SpriteFrame(-28, -28, 60, 20, 326, 139)
	SpriteFrame(-28, -28, 60, 20, 265, 160)
	SpriteFrame(-28, -28, 60, 20, 326, 160)
	SpriteFrame(-28, -28, 60, 20, 265, 181)
	SpriteFrame(-28, -28, 60, 20, 326, 181)
	SpriteFrame(-32, -8, 64, 32, 265, 202)
	SpriteFrame(-4, 24, 16, 8, 432, 178)
	SpriteFrame(-4, 24, 16, 12, 415, 170)
	SpriteFrame(-4, 24, 16, 8, 432, 178)
	SpriteFrame(-4, 24, 16, 24, 432, 153)
	SpriteFrame(-4, 24, 16, 12, 415, 157)
	SpriteFrame(-4, 24, 16, 24, 398, 157)
	SpriteFrame(-96, 0, 80, 6, 265, 235)
	SpriteFrame(-96, 6, 80, 6, 265, 241)
	SpriteFrame(3, -28, 11, 8, 437, 187)
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
